Interface (object-oriented programming)
part 5/6 · 9.9 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
trait Pet {
fn speak(&self);
}
struct Dog<'a> {
name: &'a str
}
impl<'a> Dog<'a> {
fn new(name: &'a str) -> Self {
Dog { name }
}
}
impl Pet<'a> for Dog<'a> {
fn speak(&self) {
println!("{} says 'Woof!'", self.name);
}
}
See also
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────